package com.topway.dsmflex.thread.job;

import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.URL;

import com.topway.dsmflex.Log;
import com.topway.dsmflex.model.servers.DataServerBean;
import com.topway.dsmflex.services.servers.ServersMonitorServiescs;


/**
 * 单个表同步线程
 * 
 * @author liusy 2013-05-09
 *
 */
public class ServersMonitorThread extends AbstractDataThread{

	private DataServerBean dt = null;
	private ServersMonitorServiescs serversMonitorServiescs = null;
	private Socket socket = null;
	private SocketAddress socketAddress = null;
	private     HttpURLConnection con  =null;
	private URL url =null;
	private int state = -1;
	public ServersMonitorThread(SynchroManager sm,DataServerBean dt)
	{
		super(sm,dt);
		try {
//			socket =new Socket();
//			socketAddress = new InetSocketAddress(dt.getIp(),Integer.parseInt(dt.getPort()));
	    	
		url = new URL(dt.getIp());
		}catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
        
		} catch (NumberFormatException e) {
			Log.info("正在监控服务器["+dt.getName()+"] 配置信息错误");
			e.printStackTrace();
		} 
		this.dt = dt;
		serversMonitorServiescs =ServersMonitorServiescs.getServersMonitorServiescs();
		
	}
	
	@Override
	public int SynchroData()
	{
		int _result=0;
		try {
			Log.info("正在监控服务器["+dt.getName()+"]");
			   state=dt.getServerState();
			   
		    try {

		    	con = (HttpURLConnection)url.openConnection();
	            int respCode = con.getResponseCode();
	         
	            //return respCode!=200 ? false : true;
	            if(respCode==404)
	            {
	            	//未连接
	            	if(state!=2)
	            	{
	            		dt.propertyChange(DataServerBean.SERVER_STATE,""+state,"2");
	            		serversMonitorServiescs.serversExceptionHander(dt);
	            	}
	            }else
	            {
	        		//已经连接
	            	if(state!=1)
	            	{
	            		dt.propertyChange(DataServerBean.SERVER_STATE,""+state,"1");
	            		serversMonitorServiescs.serversExceptionHander(dt);
	            	}
	            }
	        } catch (ConnectException e) {
	         	//未连接
            	if(state!=2)
            	{
            		dt.propertyChange(DataServerBean.SERVER_STATE,""+state,"2");
            		serversMonitorServiescs.serversExceptionHander(dt);
            	}
	        }

		    
		    
//			try {
//				socket.connect(socketAddress);
//				if((socket.getInputStream().read())!=-1)
//				{
//					//已经连接
//					dt.propertyChange(DataServerBean.SERVER_STATE,"0","1");
//				}
//				else{
//					//未连接
//					dt.propertyChange(DataServerBean.SERVER_STATE,"1","2");
//				}
//			} catch (ConnectException e) {
//				//未连接
//				dt.propertyChange(DataServerBean.SERVER_STATE,"1","2");
//				e.printStackTrace();
//			}
		} catch (Exception e) {
			Log.error("监控服务器["+dt.getName()+"]发生错误，忽略!"+e.getMessage());
			e.printStackTrace();
		}
		return _result;
	}
	
	
	
	public static void main(String[] args) {
		 
		String nterval  =String.valueOf((1000*60*0.5));
			System.out.println(nterval.indexOf(".")==-1?Long.parseLong(nterval):Long.parseLong(nterval.substring(0, nterval.indexOf("."))));
	}
	
	
}
